using System;
using System.Collections;
using System.Xml;

using MediaWikiEngine.Domain;

namespace MediaWikiEngine.Command
{
	/// <summary>
	/// Summary description for GetPage.
	/// </summary>
	public class GetPage : BaseCommand
	{
		public GetPage(CommandParams param, WikiConnection conn) : base(param, conn)
		{
		}

        public GetPage(WikiConnection conn, string ns, string title) : base(conn)
        {
            CommandParams parameters = new CommandParams();
            parameters["namespace"] = ns;
            parameters["title"] = title;

            _params = parameters;
        }

		/// <summary>
		/// Creates a Wiki Get Page command using an Xml text writer.
		/// </summary>
		/// <param name="inXW"></param>
		/// <param name="inNamespace"></param>
		/// <param name="inTitle"></param>
		static public void ToXml(
			XmlTextWriter inXW,
			string inNamespace,
			string inTitle)
		{
			StartCommandToXml(inXW, "wiki:getpage");

			StartConditionsToXml(inXW);
			IfConditionTestToXml(inXW, "$loginsuccess", "equals", "True", "bot:exit");
			EndConditionsToXml(inXW);

			StartInputToXml(inXW);
			InputNameValueToXml(inXW, "namespace", inNamespace);
			InputNameValueToXml(inXW, "title", inTitle);
			EndInputToXml(inXW);

			StartOutputToXml(inXW);
			OutputNameValueToXml(inXW, "pagedata", "$page");
			EndOutputToXml(inXW);

			EndCommandToXml(inXW);
		}

		public override CommandResult Execute()
		{
			WikiPage wikiPage = new WikiPage();
            wikiPage.Namespace = (string) _params["namespace"];
            wikiPage.Title = (string) _params["title"];

			wikiPage.RawData = _conn.DoGet(_conn.Endpoint.PageWithMode(wikiPage.Namespace, wikiPage.Title, "edit"));

			Hashtable results = new Hashtable();
			results["pagedata"] = wikiPage;

			return new GetPageResult(results);
		}
	}

    public class GetPageResult : CommandResult
    {
        public GetPageResult(Hashtable results)
            : base(results)
        {
        }

        public WikiPage Page
        {
            get { return GetData<WikiPage>("pagedata"); }
        }
    }
}
